package cn.shuangliang.contract.service;

import cn.shuangliang.contract.dao.ContracthistoryMapper;
import cn.shuangliang.contract.dao.ContractitemMapper;
import cn.shuangliang.contract.model.Contracthistory;
import cn.shuangliang.contract.model.Contractitem;
import cn.shuangliang.contract.util.LogUtil;
import cn.shuangliang.contract.vo.Constant;
import cn.shuangliang.contract.dao.ContractMapper;
import cn.shuangliang.contract.model.Contract;
import cn.shuangliang.contract.util.MyBatisUtil;
import cn.shuangliang.contract.vo.Page;

import java.util.Date;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */

public class ContractService {

    public Page<Contract> search(int pageNo, String no, String type, Date start, Date end) {
        try {
            ContractMapper mapper = MyBatisUtil.get().getMapper(ContractMapper.class);
            List<Contract> search = mapper.search(pageNo, Constant.pageSize, no, type, start, end);
            int total = mapper.searchTotal(no, type, start, end);
            return new Page<Contract>(search, pageNo, total, Constant.pageSize);
        } finally {
            MyBatisUtil.close();
        }
    }

    //增加
    public int add(Contract cr, List<Contractitem> list) {
        try {
            ContractMapper mapper = MyBatisUtil.get().getMapper(ContractMapper.class);
            ContractitemMapper m2 = MyBatisUtil.get().getMapper(ContractitemMapper.class);
            mapper.insertSelective(cr);
            for (Contractitem ci : list) {
                ci.setContrid(cr.getId());
                m2.insert(ci);
            }
            return 1;
        } catch (Exception e) {
            LogUtil.error("增加失败", e);
            MyBatisUtil.get().rollback();
        } finally {
            MyBatisUtil.close();
        }
        return 0;
    }

    //根据是否有id,决定是更新还是增加
    public int add(Contractitem contractitem) {
        try {
            ContractMapper mapper = MyBatisUtil.get().getMapper(ContractMapper.class);
            ContractitemMapper m2 = MyBatisUtil.get().getMapper(ContractitemMapper.class);
            if (contractitem.getId() != null&&contractitem.getId()>0) {
                return m2.updateByPrimaryKey(contractitem);
            }
            return m2.insert(contractitem);
        } finally {
            MyBatisUtil.close();
        }

    }

    //删除合同及其子项
    public int del(long id){
        try {
            ContractMapper mapper = MyBatisUtil.get().getMapper(ContractMapper.class);
            return mapper.delContract(id);
        } finally {
            MyBatisUtil.close();
        }
    }
    //查看全部的关联信息
    public Contract find(Long id) {
        try {
            ContractMapper mapper = MyBatisUtil.get().getMapper(ContractMapper.class);
            return mapper.find(id);
        } finally {
            MyBatisUtil.close();
        }

    }
    public Contract findWithHis(Long id) {
        try {
            ContractMapper mapper = MyBatisUtil.get().getMapper(ContractMapper.class);
            return mapper.findWithHis(id);
        } finally {
            MyBatisUtil.close();
        }

    }
    public int confirm(Long id, boolean b) {
        try {
            ContractMapper mapper = MyBatisUtil.get().getMapper(ContractMapper.class);
            return mapper.confirm(id,b);
        } finally {
            MyBatisUtil.close();
        }
    }

    public int addHis(Contracthistory c) {
        try {
            ContracthistoryMapper mapper = MyBatisUtil.get().getMapper(ContracthistoryMapper.class);
            ContractMapper mapper2 = MyBatisUtil.get().getMapper(ContractMapper.class);
            int r = mapper.insert(c);
            int r1 = mapper2.updateAt(c.getCid(), c.getProdid(), c.getAtweight(), c.getAtnum());

            return r * r1;
        }catch (Exception e){
            LogUtil.error(e.getMessage(),e);
            return -2;
        } finally {
            MyBatisUtil.close();
        }
    }

    public int update(Contract cr) {
        try {
            ContractMapper mapper = MyBatisUtil.get().getMapper(ContractMapper.class);
            return mapper.updateByPrimaryKeySelective(cr);
        } finally {
            MyBatisUtil.close();
        }
    }

    public int delCItem(Long id, Long cid) {
        try {
            ContractMapper mapper = MyBatisUtil.get().getMapper(ContractMapper.class);
            ContracthistoryMapper mapper2= MyBatisUtil.get().getMapper(ContracthistoryMapper.class);
            int historys=mapper.checkHistorys(id,cid);
            if(historys<1){
               return mapper2.deleteByPrimaryKey(id);
            }
            return -1;//有记录
        } finally {
            MyBatisUtil.close();
        }

    }
}
